(defpackage :boxes.util
  (:use :cl)
  (:export #:text-width
           #:text-height
	   #:center-inside
           #:version))

(in-package :boxes.util)

(defun text-width (thing font)
  (sdl:get-font-size (if (stringp thing)
			 thing
			 (prin1-to-string thing))
		     :size :w
		     :font font))

(defun text-height (thing font)
  (sdl:get-font-size (if (stringp thing)
			 thing
			 (prin1-to-string thing))
		     :size :h
		     :font font))

(defun center-inside (container-width width &optional (ofs 0))
  (+ ofs (truncate (- container-width width) 2)))

;; jump some hoops to make sure the version is always correctly
;; reported.
#+lispworks
(eval-when (:compile-toplevel :load-toplevel :execute)
  (let ((version (with-open-stream (s (system:run-shell-command
                                       (format nil "cd ~a && git log --pretty=oneline | wc -l | awk '{print $1}'"
                                               (make-pathname :name nil :type nil :defaults (or #.*compile-file-pathname* *load-pathname*)))
                                      :wait nil
                                      :output :stream))
                   (read-line s))))
    (defun version () version)))

#-(or lispworks)
(defun version () "Unknown")